#include <iostream>
#include <string>
#include <vector>
#include <sqlite3.h>
using namespace std;

class SqliteHelper
{
    typedef int (*sqlite_callback)(void *, int, char **, char **);

public:
    SqliteHelper(const string &filename)
        : _db_filename(filename), _db_handler(nullptr)
    {
    }
    bool open(int level = SQLITE_OPEN_FULLMUTEX)
    {
        int ret = sqlite3_open_v2(_db_filename.c_str(), &_db_handler, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | level, nullptr);
        if (ret != SQLITE_OK)
        {
            cout << "数据库文件创建/打开失败" << sqlite3_errmsg(_db_handler) << endl;
            return false;
        }
        return true;
    }
    bool exec(const string &sql, sqlite_callback callback, void *arg)
    {
        int ret = sqlite3_exec(_db_handler, sql.c_str(), callback, arg, nullptr);
        if (ret != SQLITE_OK)
        {
            cout << sql << endl;
            cout << "数据库操作失败" << sqlite3_errmsg(_db_handler) << endl;
            return false;
        }
        return true;
    }
    void close()
    {
        if (_db_handler)
            sqlite3_close(_db_handler);
    }

private:
    string _db_filename;
    sqlite3 *_db_handler;
};